library(tidyverse)

# Consider 3 bonds, each with 4% probability of default, independent from one another,
# assumed i.i.d.

p <- 0.04

# Each bond follows a Bernoulli distribution, so the discrete random variable
# X - nr of bonds that default follows a Binomial. Let's plot its probability mass function.

plot.data <- tibble(
  x = 0:3 # nr of bonds that default
) %>%
  mutate(
    prob = #...
    ) 
plot.data

ggplot(plot.data, aes(x = x, y = prob)) +
  geom #...
  theme_bw() +
  labs(
    x = "number of bonds that default", y = "probability of default",
    title = "Probability Mass Function for the R.V. 'Nr. of bonds that default'",
    subtitle = "3 bonds, each with 4% probability of default, i.i.d."
    )

# We easily see that the 95% VaR for the probability of default in a portfolio 
# including the three bonds is 1 (corresponding to 1 default).

# However, the 95% VaR for the prob. of a default in a portfolio including just
# one of the bonds is 0. So, according to VaR, the latter is the less risky option.

# NOTE: By default, geom_bar() comes with stat = "count", which has a different
# behaviour than stat = "identity".

